﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using Com;
using Core;
using Models;

namespace Controllers
{
    public class LoginController: AdminBaseController
    {
        public ActionResult GetInfo(int id)
        {
            return Success(new adminCore().GetSafeOne(id));
        }
//        [HttpPost]
        [AllowAnonymous]
        public ActionResult In(loginModel model)
        {
            var admincore = new adminCore();
#if !DEBUG
            //Log(model.code + " session:" + Session["code"]);
            if (Session["code"]==null||model.code.ToLower() != Session["code"].ToString().ToLower())
            {
                Com.Utils.ResetCode();
                return Error("验证码错误！");
            }
#endif
            var sysinfo = new sysinfoCore().GetOne();
            if (sysinfo != null)
            {
                if (DateTime.Now > Convert.ToDateTime(DESEncrypt.Decrypt(sysinfo.k)))
                {
                    return Error("授权到期被禁用！");
                }
            }
            else
            {
                return Error("未授权被禁用！");
            }
            var adminmodel = admincore.GetAdminByNP(model.username, model.password);
            if (adminmodel != null)
            {
                Session["admin"] = adminmodel;
                Session.Timeout = 60;
                Com.Utils.ResetCode();
                Log(adminmodel.username+"登录");
                return Success(adminmodel);
            }

            Com.Utils.ResetCode();
            return Error("用户名密码错误,或用户已过期，或账号被禁用！");
        }
        [AllowAnonymous]
        public ActionResult Out()
        {
            if (Session == null || Session["admin"] == null)
            {
                return Success("退出完成！");
            }

            Log(((adminEntity)Session["admin"]).username + "退出");
            Session.Clear();
            return Success("退出完成！");
        }

    }
}
